package com.wtz.util.learn.stringutils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.flink.types.Row;

/**
 * Date: 2020/10/14
 * Company: www.dtstack.com
 *
 * @author tiezhu
 */
public class PatternDemoOne {
    private static final Pattern PATTERN = Pattern.compile("\\$\\{([^}]*)}");
    private static final Pattern STRING_TYPE_PATTERN = Pattern.compile("cast\\((.*) as string\\)");
    private static final Pattern TIMESTAMP_TYPE_PATTERN = Pattern.compile("cast\\((.*) as timestamp\\)");
    private static final Pattern TEMP_PATTERN = Pattern.compile("cast\\((.*) as (.*)\\)");
    private static final String[] NEED_QUOTE_TYPE = {"string", "timestamp"};
    private static final Pattern[] TYPE_PATTERN = {STRING_TYPE_PATTERN, TIMESTAMP_TYPE_PATTERN};

    public static void main(String[] args) {
        // cast('tiezhu' as string), cast('2001-01-09 01:05:01' as timestamp), cast(123 as int)
        final String[] temp = {"cast('2001-01-09 01:05:01.0' as timestamp), true, cast(tiezhu as string), 1"};
//        Arrays.stream(TYPE_PATTERN).forEach(
//                pattern -> {
//                    Matcher matcher = pattern.matcher(temp[0]);
//                    while (matcher.find()) {
//                        temp[0] = temp[0].replace(matcher.group(1), "'" + matcher.group(1) + "'");
//                    }
//                }
//        );
        System.out.println(valueConditionAddQuotation(temp[0]));


//        String[] temps = temp[0].split(", ");
//        Arrays.stream(temps).forEach(
//                item -> {
//                    Matcher matcher = TEMP_PATTERN.matcher(item);
//                    while(matcher.find()) {
//                        String type = matcher.group(2);
//                        String value = matcher.group(1);
//                        if (Arrays.asList(replaceType).contains(type)) {
//                            value = "'" + value + "'";
//                        }
//                        System.out.println(value);
//                    }
//                }
//        );


//        AtomicInteger tempNum = new AtomicInteger(0);
//        Matcher matcher = PATTERN.matcher("INSERT INTO table(id, name) PARTITION(${city}, ${pt}, ${time}) VALUES(1, '张三')");
//        while (matcher.find()) {
//            tempNum.incrementAndGet();
//            System.out.println(tempNum);
//            System.out.println(matcher.group(1));
//            System.out.println("find ! " + matcher.group(1));
//        }
//
//        List<String> fieldNames = new ArrayList<>();
//        fieldNames.add("age");
//        fieldNames.add("name");
//        fieldNames.add("id");
//        fieldNames.add("city");
//
//        List<String> fieldTypes = new ArrayList<>();
//        fieldTypes.add("int");
//        fieldTypes.add("string");
//        fieldTypes.add("int");
//        fieldTypes.add("string");
//
//        String partitionFields = "age, ${city}";
//        List<String> staticPartitionFields = new ArrayList<>();
//        staticPartitionFields.add("city");
//
//        List<String> valueFields = new ArrayList<>(fieldNames);
//
//        for (int i = valueFields.size() - 1; i >= 0; i--) {
//            if (staticPartitionFields.contains(fieldNames.get(i))) {
//                valueFields.remove(i);
//                fieldTypes.remove(i);
//            }
//        }
//
//        for (int i = 0; i < valueFields.size(); i++) {
//            if (partitionFields.contains(fieldNames.get(i))) {
//                valueFields.add(valueFields.remove(i));
//                fieldTypes.add(fieldTypes.remove(i));
//            }
//        }
//        System.out.println(valueFields);
//        System.out.println(fieldTypes);
//


    }

    private static String buildValuesCondition(List<String> fieldTypes, Row row) {
        String valuesCondition = "(" + fieldTypes.stream().map(
                f -> {
                    if (Arrays.asList(NEED_QUOTE_TYPE).contains(f.toLowerCase())) {
                        return String.format("cast(? as %s)", f.toLowerCase());
                    }
                    return "?";
                }).collect(Collectors.joining(", ")) + ")";
        for (int i = 0; i < row.getArity(); i++) {
            valuesCondition = valuesCondition.replaceFirst("\\?", Objects.isNull(row.getField(i)) ? "null" : row.getField(i).toString());
        }
        Matcher matcher = TEMP_PATTERN.matcher(valuesCondition);
        while (matcher.find()) {
            valuesCondition = valuesCondition.replace(matcher.group(1), "'" + matcher.group(1) + "'");
        }
        return valuesCondition;
    }

    private static String valueConditionAddQuotation(String valueCondition) {
        final String[] valueConditionCopy = {valueCondition};
        String[] temps = valueCondition.split(",");
        Arrays.stream(temps).forEach(
                item -> {
                    Matcher matcher = TEMP_PATTERN.matcher(item);
                    while (matcher.find()) {
                        String value = matcher.group(1);
                        String type = matcher.group(2);

                        if (Arrays.asList(NEED_QUOTE_TYPE).contains(type)) {
                            if (!"null".equals(value)) {
                                valueConditionCopy[0] = valueConditionCopy[0].replace(value, "'" + value + "'");
                            }
                        }
                    }
                }
        );
        return valueConditionCopy[0];
    }
}
